package com.cbhb.sys;

import java.util.HashSet;
import java.util.Set;

public class PathCrosing {
    public static  boolean isPathCrossing(String path) {
        Set<Integer> set = new HashSet<>();
        int x = 0,y = 0;
        set.add(getHash(x, y));
        int length = path.length();
        for (int i = 0; i<length; i++) {
            char item = path.charAt(i);
            switch (item) {
                case 'N': ++y; break;
                case 'S': --y; break;
                case 'E': ++x; break;
                case 'W': --x; break;
            }
            if (!set.add(getHash(x, y))) {
                return true;
            }

        }
        return false;
    }

    public static int getHash(int x, int y) {
        // 避免hash冲突
        return x*20001 + y;
    }
}
